#include<bits/stdc++.h>
using namespace std;
const int N = 1e5+10;
const int P = N*80;
int siz[P];
int cnt[P];
int son[P][2];
int val[P];
long long sum[P];
#define ls(x) son[x][0]
#define rs(x) son[x][1]
int pcnt;
void push_up(int now){
    sum[now] = sum[ls(now)] + sum[rs(now)]; 
    val[now] = max(val[ls(now)],val[rs(now)]);
    cnt[now] = cnt[ls(now)] + cnt[rs(now)];  
    siz[now] = siz[ls(now)] + siz[rs(now)];
}
int gen(){
    int now = ++pcnt;
    siz[now] = 0;
    cnt[now] = 0;
    son[now][0] = son[now][1] = 0;
    return now;
}
int gen(int p){
    int now = gen();
    siz[now] = 1;
    cnt[now] = 1;
    sum[now] = p;
    val[now] = p;
    return now;
}
int gen(int x,int y){
    int now = gen();
    son[now][0] = x;
    son[now][1] = y;
    push_up(now);
    return now;
}
void balence(int &x){
    if(max(siz[ls(x)],siz[rs(x)]) < min(siz[ls(x)],siz[rs(x)])*3){
        return;
    }
    if(siz[ls(x)] > siz[rs(x)]){
        if(siz[rs(ls(x))] >= siz[ls(ls(x))]*2){
            x = gen(
                gen(ls(ls(x)),ls(rs(ls(x)))),
                gen(rs(rs(ls(x))),rs(x))
            );
        }else{
            x = gen(ls(ls(x)),gen(rs(ls(x)),rs(x)));
        }
    }else{
        if(siz[ls(rs(x))] >= siz[rs(rs(x))]*2){
            x = gen(
                gen(ls(x),ls(ls(rs(x)))),
                gen(rs(ls(rs(x))),rs(rs(x)))
            );
        }else{
            x = gen(gen(ls(x),ls(rs(x))),rs(rs(x)));
        }
    }
}
int copy(int cop){
    int now = gen();
    son[now][0] = son[cop][0];
    son[now][1] = son[cop][1];
    val[now] = val[cop];
    siz[now] = siz[cop];
    cnt[now] = cnt[cop];
    sum[now] = sum[cop];
    return now;
}
void insert(int &now,int v){
    if(!now){
        now = gen(v);
        return;
    }
    if(!son[now][0]){
        if(v > val[now]){
            now = gen(now,gen(v));
        }else if(v < val[now]){
            now = gen(gen(v),now);
        }else{
            now = copy(now);
            cnt[now]++;
            sum[now] += v;
        }
        return;
    }
    now = copy(now);
    if(val[ls(now)] < v){
        insert(rs(now),v);
    }else{
        insert(ls(now),v);
    }
    push_up(now);
    balence(now);
}
void del(int &now,int v){
    if(!son[now][0]){
        if(v == val[now]){
            if(cnt[now] == 1){
                now = 0;
            }else{
                now = copy(now);
                cnt[now]--;
                sum[now]-=v;
            }
        }
        return;
    }
    now = copy(now);
    if(val[ls(now)] < v){
        del(rs(now),v);
        if(!rs(now)){
            now = ls(now);
            return;
        }
    }else{
        del(ls(now),v);
        if(!ls(now)){
            now = rs(now);
            return;
        }
    }
    push_up(now);
    balence(now);
}
long long nth(int now,int rnk){
    if(!son[now][0]){
        if(rnk >= cnt[now]){
            return sum[now];
        }else{
            return 1ll*rnk*val[now];
        }
    }
    if(cnt[ls(now)] >= rnk){
        return nth(ls(now),rnk);
    }else{
        return nth(rs(now),rnk - cnt[ls(now)]) + sum[ls(now)];
    }
}
vector<int> ins[N];
vector<int> rem[N];
int rot[N];
int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int m,n;
    cin>>m>>n;
    for(int i = 1;i<=m;i++){
        int s,e,p;
        cin>>s>>e>>p;
        ins[s].push_back(p);
        rem[e + 1].push_back(p);
    }
    for(int i = 1;i<=n;i++){
        rot[i] = rot[i - 1];
        for(int v : ins[i])
        {
            insert(rot[i],v);
        }
        for(int v : rem[i]){
            del(rot[i],v);
        }
    }
    long long pre = 1;
    for(int i = 1;i<=n;i++){
        int x,a,b,c;
        cin>>x>>a>>b>>c;
        int k = 1 + (pre%c*a%c + b)%c;
        pre = nth(rot[x],k);
        cout<<pre<<'\n';
    }
}
